#!/usr/bin/python
#
#    Copyright 2006 Andrew Wilkinson <aw@cs.york.ac.uk>.
#
#    This file is part of PyScobble (http://lastfm.indiegigs.co.uk/pyscrobble)
#
#    PyScobble is free software; you can redistribute it and/or modify
#    it under the terms of the GNU Lesser General Public License as published by
#    the Free Software Foundation; either version 2.1 of the License, or
#    (at your option) any later version.
#
#    PyScobble is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Lesser General Public License for more details.
#
#    You should have received a copy of the GNU Lesser General Public License
#    along with PyScobble; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

import pydcop
import scrobble
import sys

def getLabels(amarok, url):
    labels = amarok.collection.query("SELECT id, name FROM labels")
    labels = dict([(int(labels[x]), labels[x+1]) for x in range(0, len(labels), 2)])

    return [labels[int(x)] for x in amarok.collection.query("SELECT labelid FROM tags_labels WHERE url='%s'" % (url, ))]

def main():
    amarok = pydcop.anyAppCalled("amarok")
    if not amarok:
        print "Amarok not found with dcop. Is it running?"
        sys.exit(1)

    artists = amarok.collection.query("SELECT id, name FROM artist;")
    artists = dict([(int(artists[x]), artists[x+1]) for x in range(0, len(artists), 2)])
    print "Got %i artists." % (len(artists), )

    albums = amarok.collection.query("SELECT id, name FROM album;")
    albums = dict([(int(albums[x]), albums[x+1]) for x in range(0, len(albums), 2)])
    print "Got %i albums." % (len(albums), )

    #tracks = amarok.collection.query("SELECT url FROM tags;")
    #print "Got %i tracks." % (len(tracks), )

    print "What type of merge do you wish to perform?"
    print "1. Last.fm authorative, 2. Amarok authorative, 3. Positive merge, 4. Negative merge"
    print "? for help"

    while True:
        mode = raw_input("> ").strip()
        if mode == "?":
            print """1. Last.fm authorative
   Make the tags in Amarok exactly match those in Last.fm.
2. Amarok authorative
   Make the tags at Last.fm exactly match those in Amarok.
3. Positive merge
   For each track in Amarok's collection, if either Last.fm or Amarok has tag apply it to both.
4. Negative merge
   For each track in Amarok's collection, remove all tags where only one of Last.fm or Amarok has it.
"""
            continue
        try:
            mode = int(mode)
        except ValueError:
            pass
        else:
            if mode >= 1 and mode <= 4:
                break

    for aid in artists:
        a = artists[aid]
        tracks = amarok.collection.query("SELECT url, title FROM tags WHERE artist=%i" % (aid, ))
        tracks = dict([(tracks[x], tracks[x+1]) for x in range(0, len(tracks), 2)])
        print "Got %i tracks for %s." % (len(tracks), a)

        tlabels = {}
        for t in tracks:
            tlabels[t] = getLabels(amarok, t)

        labels = {}
        for t in tlabels:
            for l in tlabels[t]:
                labels[l] = labels.get(l, 0) + 1

        for l in labels:
            if labels[l] == len(tracks):
                print l, "is an artist label."

if __name__ == "__main__":
    main()
